package com.ciwong.epaper.modules.epaper.ui;

import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.TimeoutError;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.ciwong.ciwongwrite.utils.CWUtils;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.bookstore.bean.PubLishClassBean;
import com.ciwong.epaper.modules.bookstore.bean.PublishStudentBean;
import com.ciwong.epaper.modules.bookstore.bean.PublishWorkContent;
import com.ciwong.epaper.modules.bookstore.bean.PublishWorkResult;
import com.ciwong.epaper.modules.bookstore.bean.StudentClassBean;
import com.ciwong.epaper.modules.bookstore.bean.StudentGroupBean;
import com.ciwong.epaper.modules.clazz.bean.StudentBean;
import com.ciwong.epaper.modules.clazz.bean.TeacherClassBean;
import com.ciwong.epaper.modules.epaper.adapter.PublishWorkClassNameAdapter;
import com.ciwong.epaper.modules.epaper.adapter.PublishWorkContentListAdapter;
import com.ciwong.epaper.modules.epaper.bean.ModuleInfo;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.epaper.util.ClickUtil;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.epaper.modules.me.bean.FavoritesBook;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.DisplayUtils;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.RequestCode;
import com.ciwong.epaper.util.SerializableManager;
import com.ciwong.epaper.widget.NoScrollListView;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.ciwong.mobilelib.bean.SchoolDetail;
import com.ciwong.mobilelib.bean.UserInfoBase;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.i.ViewOnClickListener;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ToastUtil;
import com.ciwong.mobilelib.widget.BottomMenuDialog;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 发布作业页面
 */
public class TeaPublishWorkActivity extends BaseActivity implements View.OnClickListener {

    private CheckBox choose_timing_publish;

    private TextView timing_publish_text;

    private TextView work_eff_time_text, work_eff_time_title;

    private TimePickerView pvTime;

    private ImageView publish_work_go_back;

    private Integer hour;

    private NoScrollListView publish_work_class_list;

    private LinearLayout choose_work_condition_lay;

    private PublishWorkClassNameAdapter publishWorkClassNameAdapter;
    private PublishWorkContentListAdapter publishWorkContentListAdapter;

    private RelativeLayout publish_work_mode_lay;
    private TextView publish_work_mode_tetx;
    private static long lastClickTime;
    //班级集合
    private List<TeacherClassBean> teacherClassBeans;
    private String clientId;
    private String userId;
    private String brandId;
    private int userRole = 2;
    //作业名
    private String workName;
    private EditText publish_work_name;
    //学生提交后可看详情
    private CheckBox choose_work_methed1;
    private CheckBox choose_work_methed2;
    private EditText publish_work_score_edit;
    private ListView publish_work_content_list;
    private TextView publish_work_auto_refuse_num;
    private int JUMP_TO_CHOOSE_GROUP = 10001;

    //跳转作业列表
    public static final int JUMP_SORECE_TYPE_ME_SETTING = 102;

    //学生信息集合
    private List<StudentGroupBean> studentBeanLists = new ArrayList<>();

    private List<StudentClassBean> studentClassBeanList = new ArrayList<>();

    private BottomMenuDialog bottomMenuDialog;
    private BottomMenuDialog refusebottomMenuDialog;
    private RelativeLayout publish_work_lay;
    private RelativeLayout choose_work_score_lay;
    private CheckBox choose_work_score;
    private RelativeLayout choose_work_submit_lay;
    private CheckBox choose_work_submit;
    private ImageView upe_work_name_img;
    private RadioGroup radio_group;
    private RadioGroup radio_group_defaut_time;
    private RadioButton defaut_time_today_button;
    private RadioButton defaut_time_oneday_button;
    private RadioButton defaut_time_twoday_button;
    private TextView mode_tips_text;
    private RelativeLayout see_little_question_layout;
    private CheckBox see_little_question_check;
    private TextView see_little_question_text;
    private RelativeLayout radio_layout;
    private RelativeLayout look_detail_layout;
    private RelativeLayout publish_work_score_layout;
    private RelativeLayout choose_work_seework_lay;
    private CheckBox choose_work_seework;
    private RelativeLayout refuseNum_layout;
    //作业模式：0 练习模式，1为机房模式，2考试模式
    private int examMode;
    //查看类型：0 提交立马能查看，1截止时间到后查看，2一直不能查看。
    private int seeWorkMode = 0;
    //补交类型：0 截止时间到后能补交 1截止时间到后不能补交(先写死后续修改)
    private int submitMode = 0;
    //显示分数类型：0 截止时间前显示（默认），1截止时间前不显示，2一直不显示
    private int showScoreMode = 0;
    //	是否自动退回模式，1-需要自动退回 0-不需要自动退回。
    private int autoRefuseMode = 0;
    //自动退回用户设定的分值百分比数字值
    private int autoRefusePercent = 50;
    //	定时发布时间.默认为 0,代表直接布置
    private long sendDate = 0;
    private EApplication application;
    //用户信息
    private UserInfoBase userInfo;
    //固定为1
    private int workMessageType = 1;
    //班级成员列表
    private List<StudentBean> studentBeans;
    //书名
    private String bookName;

    //班级集合,当布置方式为个人时,此处为所选学生所在班级.布置方式为班级时,此处是所选班级集合
    private List<PubLishClassBean> pubLishClassBeanList = new ArrayList<>();

    //老师名称
    private String publishUserName;

    //用户自定义服务id
    private int workType = 0;

    //	作业完成时间
    private long completeDate;

    //布置类型：0未知 1班级布置 2小组布置 4个人布置
    private int publishType = 0;

    private List<PublishWorkContent> publishWorkContentList;
    //	科目 ID
    private int subjectId;

    /**
     * 时间类型  1为截止时间 2为发布时间
     */
    private int timingOrEff = 1;

    /**
     * 是否勾选定时发布  0否  1勾选
     */
    private int isChickTime = 0;


    //ab卷模式
    private int abMode;
    /**
     * 是否是课堂作业
     */
    private boolean isClassWork = false;
    /**
     * 学校id
     */
    private int schoolId;
    /**
     * 学校名
     */
    private String schoolName;
    /**
     * 地区码
     */
    private String areaCode;
    /**
     * 用于分辨布置的哪个作业 默认课后练习
     */
    private String tag = RequestCode.TeaWorkContent.HOME_WORK;
    //0 截止时间前显示；1截止时间前不显示，截止时间后显示；2一直不显示
    private int showAnswerMode = 0;

    //默认过期天数
    private int default_eff_day = 1;

    private RadioButton exercise_button;

    private int autoRefuseNum = 2;

    @Override
    protected int setView() {
        return R.layout.activity_publish_work;
    }

    @Override
    protected void findViews() {
        choose_timing_publish = findViewById(R.id.choose_timing_publish);
        timing_publish_text = findViewById(R.id.timing_publish_text);
        work_eff_time_text = findViewById(R.id.work_eff_time_text);
        work_eff_time_title = findViewById(R.id.work_eff_time_title);
        publish_work_class_list = findViewById(R.id.publish_work_class_list);
        publish_work_mode_lay = findViewById(R.id.publish_work_mode_lay);
        publish_work_mode_tetx = findViewById(R.id.publish_work_mode_tetx);
        publish_work_name = findViewById(R.id.publish_work_name);
        publish_work_lay = findViewById(R.id.publish_work_lay);
        choose_work_methed1 = findViewById(R.id.choose_work_methed1);
        choose_work_methed2 = findViewById(R.id.choose_work_methed2);
        publish_work_score_edit = findViewById(R.id.publish_work_score_edit);
        publish_work_auto_refuse_num = findViewById(R.id.publish_work_auto_refuse_num);
        publish_work_content_list = findViewById(R.id.publish_work_content_list);
        choose_work_condition_lay = findViewById(R.id.choose_work_condition_lay);
        choose_work_score_lay = findViewById(R.id.choose_work_score_lay);
        choose_work_score = findViewById(R.id.choose_work_score);
        choose_work_submit_lay = findViewById(R.id.choose_work_submit_lay);
        choose_work_submit = findViewById(R.id.choose_work_submit);
        choose_work_score = findViewById(R.id.choose_work_score);
        upe_work_name_img = findViewById(R.id.upe_work_name_img);
        radio_group = findViewById(R.id.radio_group);
        radio_group_defaut_time = findViewById(R.id.radio_group_defaut_time);
        defaut_time_today_button = findViewById(R.id.defaut_time_today_button);
        defaut_time_oneday_button = findViewById(R.id.defaut_time_oneday_button);
        defaut_time_twoday_button = findViewById(R.id.defaut_time_twoday_button);
        mode_tips_text = findViewById(R.id.mode_tips_text);
        exercise_button = findViewById(R.id.exercise_button);
        see_little_question_layout = findViewById(R.id.see_little_question_layout);
        see_little_question_check = findViewById(R.id.see_little_question_check);
        see_little_question_text = findViewById(R.id.see_little_question_text);
        radio_layout = findViewById(R.id.radio_layout);
        refuseNum_layout = findViewById(R.id.refuseNum_layout);
        look_detail_layout = findViewById(R.id.look_detail_layout);
        publish_work_score_layout = findViewById(R.id.publish_work_score_layout);
        choose_work_seework_lay = findViewById(R.id.choose_work_seework_lay);
        choose_work_seework = findViewById(R.id.choose_work_seework);
        choose_timing_publish.setOnClickListener(this);
        timing_publish_text.setOnClickListener(this);
        work_eff_time_text.setOnClickListener(this);
        publish_work_mode_lay.setOnClickListener(this);
        publish_work_auto_refuse_num.setOnClickListener(this);
        publish_work_lay.setOnClickListener(this);
        upe_work_name_img.setOnClickListener(this);
        setCheckClick();
    }

    /**
     * 设置checkBox点击事件
     */
    public void setCheckClick() {
        choose_work_methed1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!compoundButton.isPressed()) {
                    return;
                }
                //学生提交后可看详情
                if (b) {
                    seeWorkMode = 0;
                    showScoreMode = 0;
                    choose_work_methed1.setChecked(true);
                    choose_work_score.setChecked(true);
                    if (choose_work_seework.isChecked()) {
                        choose_work_seework.setChecked(false);
                        showAnswerMode = 0;
                    }
                } else {
                    seeWorkMode = 1;
                    showAnswerMode = 1;
                    see_little_question_check.setChecked(false);
                    choose_work_methed1.setChecked(false);
                }
            }
        });

        see_little_question_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!compoundButton.isPressed()) {
                    return;
                }
                //截止前查看小题答案
                if (b) {
                    if (examMode == 1) {
                        //机房
                        showAnswerMode = 2;
                    } else {
                        showAnswerMode = 0;
                        seeWorkMode = 0;
                        showScoreMode = 0;
                        choose_work_score.setChecked(true);
                        choose_work_methed1.setChecked(true);
                        if (choose_work_seework.isChecked()) {
                            choose_work_seework.setChecked(false);
                        }
                    }
                } else {
                    if (examMode == 1) {
                        //机房
                        if (choose_work_seework.isChecked()) {
                            choose_work_seework.setChecked(false);
                        }
                        seeWorkMode = 1;
                        showScoreMode = 1;
                    }
                    showAnswerMode = 1;
                }
            }
        });

        choose_work_methed2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!compoundButton.isPressed()) {
                    return;
                }
                //低于满分
                if (b) {
                    autoRefuseMode = 1;
                    choose_work_methed2.setChecked(true);
                    autoRefusePercent = Integer.parseInt(publish_work_score_edit.getText().toString());
                } else {
                    autoRefuseMode = 0;
                    choose_work_methed2.setChecked(false);
                    autoRefusePercent = 0;
                }
            }
        });
        choose_work_seework.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!compoundButton.isPressed()) {
                    return;
                }
                //学生不可看分数答案（截止后也不可看）
                if (b) {
                    choose_work_methed1.setChecked(false);
                    choose_work_score.setChecked(false);
                    if (examMode == 1) {
                        see_little_question_check.setChecked(true);
                    } else {
                        see_little_question_check.setChecked(false);
                    }
                    showAnswerMode = 2;
                    seeWorkMode = 2;
                    showScoreMode = 2;
                } else {
                    if (examMode == 1) {
                        seeWorkMode = 1;
                        showScoreMode = 1;
                    } else {
                        showAnswerMode = 0;
                        seeWorkMode = 0;
                        showScoreMode = 0;
                    }
                }
            }
        });
        //设置退回比列
        publish_work_score_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    autoRefusePercent = Integer.parseInt(publish_work_score_edit.getText().toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        choose_work_submit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!compoundButton.isPressed()) {
                    return;
                }
                //截止时间后学生可补交
                if (b) {
                    submitMode = 0;
                    choose_work_submit.setChecked(true);
                } else {
                    submitMode = 1;
                    choose_work_submit.setChecked(false);
                }

            }
        });

        choose_work_score.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!compoundButton.isPressed()) {
                    return;
                }
                //学生提交后可看分数
                if (b) {
                    showScoreMode = 0;
                    choose_work_score.setChecked(true);
                    if (choose_work_seework.isChecked()) {
                        choose_work_seework.setChecked(false);
                        showAnswerMode = 0;
                        seeWorkMode = 0;
                    }
                } else {
                    showScoreMode = 1;
                    seeWorkMode = 1;
                    showAnswerMode = 1;
                    see_little_question_check.setChecked(false);
                    choose_work_score.setChecked(false);
                    choose_work_methed1.setChecked(false);
                }
            }
        });

        choose_timing_publish.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //定时布置
                if (b) {
                    //勾选 定时布置
                    choose_timing_publish.setChecked(true);
                    timing_publish_text.setVisibility(View.VISIBLE);
                    isChickTime = 1;
                    sendDate = (System.currentTimeMillis() + 3600000) / 1000;
                    initEffDate(false);
                    setTime();
                    timing_publish_text.setText(CWUtils.formatDate7(System.currentTimeMillis() + 3600000));
                } else {
                    //取消勾选 定时布置
                    sendDate = 0;
                    isChickTime = 0;
                    choose_timing_publish.setChecked(false);
                    timing_publish_text.setVisibility(View.GONE);
                    initEffDate(false);
                }
            }
        });
    }

    @Override
    protected void initEvent() {
//        publish_work_auto_refuse_num.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                autoRefuseNum = i + 1;
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> adapterView) {
//
//            }
//        });
        radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton button = (RadioButton) radioGroup.findViewById(i);
                String text = button.getText().toString();
                if (text.equals(getResources().getString(R.string.ceshi_mode))) {
                    //测试
                    choose_work_condition_lay.setVisibility(View.VISIBLE);
                    examMode = 2;
                    submitMode = 0;
                    showScoreMode = 0;
                    //查看类型：0 提交立马能查看，1截止时间到后查看
                    seeWorkMode = 1;
                    //	是否自动退回模式，1-需要自动退回 0-不需要自动退回。
                    autoRefuseMode = 0;
                    showAnswerMode = 1;
                    choose_work_score.setChecked(true);
                    choose_work_submit.setChecked(true);
                    see_little_question_check.setChecked(false);
                    choose_work_methed1.setChecked(false);
                    choose_work_methed2.setChecked(false);
                    choose_work_seework.setChecked(false);
                    choose_work_seework_lay.setVisibility(View.VISIBLE);
                    choose_work_score_lay.setVisibility(View.VISIBLE);
                    choose_work_submit_lay.setVisibility(View.VISIBLE);
                    see_little_question_layout.setVisibility(View.VISIBLE);
                    mode_tips_text.setText(getResources().getString(R.string.ceshi_mode_tips));
                    work_eff_time_title.setText("进场截止");
                } else {
                    //练习
                    examMode = 0;
                    //查看类型：0 提交立马能查看，1截止时间到后查看
                    seeWorkMode = 0;
                    //	是否自动退回模式，1-需要自动退回 0-不需要自动退回。
                    autoRefuseMode = 0;
                    submitMode = 0;
                    showScoreMode = 0;
                    showAnswerMode = 0;
                    choose_work_methed1.setChecked(true);
                    see_little_question_check.setChecked(true);
                    choose_work_methed2.setChecked(false);
                    choose_work_seework.setChecked(false);
                    choose_work_seework_lay.setVisibility(View.GONE);
                    choose_work_condition_lay.setVisibility(View.VISIBLE);
                    choose_work_score_lay.setVisibility(View.GONE);
                    choose_work_submit_lay.setVisibility(View.GONE);
                    see_little_question_layout.setVisibility(View.VISIBLE);
                    mode_tips_text.setText(getResources().getString(R.string.lianxi_mode_tips));
                    work_eff_time_title.setText("截止时间");
                }

            }
        });
        radio_group_defaut_time.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                try {
                    if (i == R.id.defaut_time_today_button) {
                        //单天
                        CWSys.setSharedInt("default_eff_day", 0);
                    } else if (i == R.id.defaut_time_oneday_button) {
                        //1天
                        CWSys.setSharedInt("default_eff_day", 1);
                    } else if (i == R.id.defaut_time_twoday_button) {
                        //2天
                        CWSys.setSharedInt("default_eff_day", 2);
                    }
                    initEffDate(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    class BottomClickListener extends ViewOnClickListener {
        private int index;

        public BottomClickListener(int index) {
            this.index = index;
        }

        @Override
        public void avertRepeatOnClick(View v) {
            switch (index) {
                case 0:
                    tag = RequestCode.TeaWorkContent.CLASS_WORK;
                    isClassWork = true;
                    examMode = 0;
                    //查看类型：0 提交立马能查看，1截止时间到后查看
                    seeWorkMode = 0;
                    //	是否自动退回模式，1-需要自动退回 0-不需要自动退回。
                    autoRefuseMode = 0;
                    submitMode = 0;
                    showScoreMode = 0;
                    choose_work_condition_lay.setVisibility(View.GONE);
                    publish_work_mode_tetx.setText(getResources().getString(R.string.class_work_mode));
                    break;
                case 1:
                    tag = RequestCode.TeaWorkContent.HOME_WORK;
                    isClassWork = false;
                    examMode = 0;
                    //查看类型：0 提交立马能查看，1截止时间到后查看
                    seeWorkMode = 0;
                    //	是否自动退回模式，1-需要自动退回 0-不需要自动退回。
                    autoRefuseMode = 0;
                    submitMode = 0;
                    showScoreMode = 0;
                    showAnswerMode = 0;
                    see_little_question_check.setChecked(true);
                    choose_work_methed1.setChecked(true);
                    choose_work_methed2.setChecked(false);
                    exercise_button.setChecked(true);
                    choose_work_condition_lay.setVisibility(View.VISIBLE);
                    choose_work_submit_lay.setVisibility(View.GONE);
                    radio_layout.setVisibility(View.VISIBLE);
                    look_detail_layout.setVisibility(View.VISIBLE);
                    see_little_question_layout.setVisibility(View.VISIBLE);
                    choose_work_score_lay.setVisibility(View.GONE);
                    publish_work_score_layout.setVisibility(View.VISIBLE);
                    refuseNum_layout.setVisibility(View.VISIBLE);
                    choose_work_seework_lay.setVisibility(View.GONE);
                    see_little_question_text.setText(R.string.see_little_question_after_eff);
                    mode_tips_text.setText(getResources().getString(R.string.lianxi_mode_tips));
                    publish_work_mode_tetx.setText(getResources().getString(R.string.home_work_mode));
                    work_eff_time_title.setText("截止时间");
                    break;
                case 2:
                    tag = RequestCode.TeaWorkContent.COMPUTER_ROOM_EXAM;
                    isClassWork = false;
                    //机房模式仅使用听说模考 在线作答 判断是否有其他题型
                    int countListenSpeak = 0;
                    int countOnlineAnswer = 0;
                    for (int i = 0; i < publishWorkContentList.size(); i++) {
                        PublishWorkContent publishWorkContent = publishWorkContentList.get(i);
                        if (publishWorkContent.getModuleId() != ModuleInfo.ModuleInfoId.MODULE_ID_LISTEN_SPEAK_EXAM
                                && publishWorkContent.getModuleId() != ModuleInfo.ModuleInfoId.MODULE_ID_ONLINE_ANSWER) {
                            ToastUtil.INSTANCE.toastCenterError(R.string.choose_work_mode);
                            return;
                        } else if (publishWorkContent.getModuleId() == ModuleInfo.ModuleInfoId.MODULE_ID_LISTEN_SPEAK_EXAM) {
                            countListenSpeak++;
                        } else if (publishWorkContent.getModuleId() == ModuleInfo.ModuleInfoId.MODULE_ID_ONLINE_ANSWER) {
                            countOnlineAnswer++;
                        }
                    }
                    if (countListenSpeak > 0 && countOnlineAnswer > 0) {
                        ToastUtil.INSTANCE.toastCenterError(R.string.choose_work_mode_tips);
                        return;
                    }
                    if (publishWorkContentList.size() > 1) {
                        abMode = 1;
                    }
                    choose_work_condition_lay.setVisibility(View.VISIBLE);
                    radio_layout.setVisibility(View.GONE);
                    look_detail_layout.setVisibility(View.GONE);
                    see_little_question_layout.setVisibility(View.VISIBLE);
                    choose_work_seework_lay.setVisibility(View.VISIBLE);
                    choose_work_score_lay.setVisibility(View.GONE);
                    choose_work_submit_lay.setVisibility(View.GONE);
                    publish_work_score_layout.setVisibility(View.GONE);
                    refuseNum_layout.setVisibility(View.GONE);
                    see_little_question_check.setChecked(false);
                    choose_work_seework.setChecked(false);
                    see_little_question_text.setText(R.string.can_not_see_answer);
                    work_eff_time_title.setText("截止时间");
                    showAnswerMode = 1;
                    examMode = 1;
                    submitMode = 1;
                    //截止前可看
                    seeWorkMode = 1;
                    showScoreMode = 1;
                    autoRefuseMode = 0;
                    publish_work_mode_tetx.setText(getResources().getString(R.string.computer_room_exam));
                    break;
                default:
                    break;
            }
            bottomMenuDialog.dismiss();
        }
    }

    class RefuseBottomClickListener extends ViewOnClickListener {
        private int index;

        public RefuseBottomClickListener(int index) {
            this.index = index;
        }

        @Override
        public void avertRepeatOnClick(View v) {
            switch (index) {
                case 0:
                    publish_work_auto_refuse_num.setText("1");
                    autoRefuseNum = 1;
                    break;
                case 1:
                    publish_work_auto_refuse_num.setText("2");
                    autoRefuseNum = 2;
                    break;
                case 2:
                    publish_work_auto_refuse_num.setText("3");
                    autoRefuseNum = 3;
                    break;
                case 3:
                    publish_work_auto_refuse_num.setText("4");
                    autoRefuseNum = 4;
                    break;
                case 4:
                    publish_work_auto_refuse_num.setText("5");
                    autoRefuseNum = 5;
                    break;
                default:
                    break;
            }
            refusebottomMenuDialog.dismiss();
        }
    }

    /**
     * 选择退回次数弹框
     */
    private void setRefuseBottomMenuDialog() {
        refusebottomMenuDialog = new BottomMenuDialog(this);

        String auto_refuse_num1 = this.getResources().getString(R.string.auto_refuse_num1);
        String auto_refuse_num2 = this.getResources().getString(R.string.auto_refuse_num2);
        String auto_refuse_num3 = this.getResources().getString(R.string.auto_refuse_num3);
        String auto_refuse_num4 = this.getResources().getString(R.string.auto_refuse_num4);
        String auto_refuse_num5 = this.getResources().getString(R.string.auto_refuse_num5);

        SpannableStringBuilder auto_refuse_num1_mode = new SpannableStringBuilder(auto_refuse_num1);
        SpannableStringBuilder auto_refuse_num2_mode = new SpannableStringBuilder(auto_refuse_num2);
        SpannableStringBuilder auto_refuse_num3_mode = new SpannableStringBuilder(auto_refuse_num3);
        SpannableStringBuilder auto_refuse_num4_mode = new SpannableStringBuilder(auto_refuse_num4);
        SpannableStringBuilder auto_refuse_num5_mode = new SpannableStringBuilder(auto_refuse_num5);
        SpannableStringBuilder cancel = new SpannableStringBuilder(this.getResources().getString(R.string.cancel));

        ForegroundColorSpan GreenSpan = new ForegroundColorSpan(Color.parseColor("#313944"));

        auto_refuse_num1_mode.setSpan(GreenSpan, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        auto_refuse_num2_mode.setSpan(GreenSpan, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        auto_refuse_num3_mode.setSpan(GreenSpan, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        auto_refuse_num4_mode.setSpan(GreenSpan, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        auto_refuse_num5_mode.setSpan(GreenSpan, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//        //设置字号
//        auto_refuse_num1_mode.setSpan(sizeSpan, 5, publish_class_work.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//        publish_home_work_mode.setSpan(GreenSpan, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//        publish_home_work_mode.setSpan(sizeSpan, 5, publish_home_work.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//        publish_work_engine_room_mode.setSpan(GreenSpan, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//        publish_work_engine_room_mode.setSpan(sizeSpan, 5, publish_work_engine_room.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

        refusebottomMenuDialog
                .setBtnTextColor(getResources().getColor(R.color.publish_work_dialog));
        refusebottomMenuDialog
                .setLastBtnTextColor(getResources().getColor(R.color.color_light_green));

        ArrayList<SpannableStringBuilder> str = new ArrayList<>();

        str.add(auto_refuse_num1_mode);
        str.add(auto_refuse_num2_mode);
        str.add(auto_refuse_num3_mode);
        str.add(auto_refuse_num4_mode);
        str.add(auto_refuse_num5_mode);
        str.add(cancel);
        refusebottomMenuDialog.setPublishMenuTextList(str);

        ArrayList<View.OnClickListener> listeners = new ArrayList<>();
        listeners.add(new TeaPublishWorkActivity.RefuseBottomClickListener(0));
        listeners.add(new TeaPublishWorkActivity.RefuseBottomClickListener(1));
        listeners.add(new TeaPublishWorkActivity.RefuseBottomClickListener(2));
        listeners.add(new TeaPublishWorkActivity.RefuseBottomClickListener(3));
        listeners.add(new TeaPublishWorkActivity.RefuseBottomClickListener(4));
        listeners.add(new TeaPublishWorkActivity.RefuseBottomClickListener(5));
        refusebottomMenuDialog.setPublishMenuOnClickListeners(listeners);
    }

    /**
     * 选择作业模式弹框
     */
    private void setBottomMenuDialog() {
        bottomMenuDialog = new BottomMenuDialog(this);

        String publish_class_work = this.getResources().getString(R.string.publish_class_work_mode);
        String publish_home_work = this.getResources().getString(R.string.publish_home_work_mode);
        String publish_work_engine_room = this.getResources().getString(R.string.publish_work_engine_room_mode);
        SpannableStringBuilder publish_class_work_mode = new SpannableStringBuilder(publish_class_work);
        SpannableStringBuilder publish_home_work_mode = new SpannableStringBuilder(publish_home_work);
        SpannableStringBuilder publish_work_engine_room_mode = new SpannableStringBuilder(publish_work_engine_room);
        SpannableStringBuilder cancel = new SpannableStringBuilder(this.getResources().getString(R.string.cancel));

        ForegroundColorSpan GreenSpan = new ForegroundColorSpan(Color.parseColor("#313944"));
        AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan(DisplayUtils.sp2px(this, 13));

        publish_class_work_mode.setSpan(GreenSpan, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        //设置字号
        publish_class_work_mode.setSpan(sizeSpan, 5, publish_class_work.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        publish_home_work_mode.setSpan(GreenSpan, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        publish_home_work_mode.setSpan(sizeSpan, 5, publish_home_work.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        publish_work_engine_room_mode.setSpan(GreenSpan, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        publish_work_engine_room_mode.setSpan(sizeSpan, 5, publish_work_engine_room.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

        bottomMenuDialog
                .setBtnTextColor(getResources().getColor(R.color.publish_work_dialog));
        bottomMenuDialog
                .setLastBtnTextColor(getResources().getColor(R.color.color_light_green));

        ArrayList<SpannableStringBuilder> str = new ArrayList<>();

        str.add(publish_class_work_mode);
        str.add(publish_home_work_mode);
        str.add(publish_work_engine_room_mode);
        str.add(cancel);
        bottomMenuDialog.setPublishMenuTextList(str);

        ArrayList<View.OnClickListener> listeners = new ArrayList<>();

        listeners.add(new TeaPublishWorkActivity.BottomClickListener(0));
        listeners.add(new TeaPublishWorkActivity.BottomClickListener(1));
        listeners.add(new TeaPublishWorkActivity.BottomClickListener(2));
        listeners.add(new TeaPublishWorkActivity.BottomClickListener(3));

        bottomMenuDialog.setPublishMenuOnClickListeners(listeners);
    }

    @Override
    protected void init() {
        setTitleText("发布作业");
        setBottomMenuDialog();
        setRefuseBottomMenuDialog();
        Intent intent = getIntent();
        //workName = intent.getStringExtra(IntentFlag.INTENT_FLAG_STR);
        tag = intent.getStringExtra(IntentFlag.INTENT_FLAG_TAG);
        bookName = intent.getStringExtra(IntentFlag.INTENT_FLAG_BOOK_NAME);
        workType = intent.getIntExtra(IntentFlag.INTENT_FLAG_WORK_TYPE, 0);
        publishWorkContentList = (List<PublishWorkContent>) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ_LIST);
        subjectId = intent.getIntExtra(IntentFlag.INTENT_FLAG_SUBJECTID, 0);
        setPageCheckStatus();
        setWorkMode();
        initEffDate(true);
        application = (EApplication) this.getApplication();
        userInfo = application.getUserInfoBase();
        if (null != userInfo) {
            publishUserName = userInfo.getRealName();
        } else {
            publishUserName = "";
        }
        publishWorkClassNameAdapter = new PublishWorkClassNameAdapter(teacherClassBeans, studentClassBeanList, studentBeanLists, TeaPublishWorkActivity.this);
        publish_work_class_list.setAdapter(publishWorkClassNameAdapter);
        publishWorkContentListAdapter = new PublishWorkContentListAdapter(publishWorkContentList, TeaPublishWorkActivity.this);
        publish_work_content_list.setAdapter(publishWorkContentListAdapter);
    }

    private void initEffDate(boolean isSetButtonStatus) {
        try {
            default_eff_day = CWSys.getSharedInt("default_eff_day", 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (isSetButtonStatus) {
            if (default_eff_day == 0) {
                defaut_time_today_button.setChecked(true);
            } else if (default_eff_day == 2) {
                defaut_time_twoday_button.setChecked(true);
            }
        }
        long effTime = System.currentTimeMillis();
        if (sendDate > 0) {
            effTime = sendDate * 1000;
        }
        Date nowaDays = new Date(effTime + default_eff_day * 24 * 60 * 60 * 1000);
        setEffDate(nowaDays);
    }

    /**
     * 设置作业模式
     */
    private void setWorkMode() {
        switch (tag) {
            case RequestCode.TeaWorkContent.CLASS_WORK:
                //tag = RequestCode.TeaWorkContent.CLASS_WORK;
                isClassWork = true;
                examMode = 0;
                //查看类型：0 提交立马能查看，1截止时间到后查看
                seeWorkMode = 0;
                //	是否自动退回模式，1-需要自动退回 0-不需要自动退回。
                autoRefuseMode = 0;
                submitMode = 0;
                showScoreMode = 0;
                showAnswerMode = 0;
                see_little_question_text.setText(R.string.see_little_question_after_eff);
                choose_work_condition_lay.setVisibility(View.GONE);
                publish_work_mode_tetx.setText(getResources().getString(R.string.class_work_mode));
                break;
            case RequestCode.TeaWorkContent.HOME_WORK:
                isClassWork = false;
                examMode = 0;
                //查看类型：0 提交立马能查看，1截止时间到后查看
                seeWorkMode = 0;
                //	是否自动退回模式，1-需要自动退回 0-不需要自动退回。
                autoRefuseMode = 0;
                submitMode = 0;
                showScoreMode = 0;
                showAnswerMode = 0;
                see_little_question_check.setChecked(true);
                choose_work_methed1.setChecked(true);
                choose_work_methed2.setChecked(false);
                exercise_button.setChecked(true);
                choose_work_condition_lay.setVisibility(View.VISIBLE);
                choose_work_submit_lay.setVisibility(View.GONE);
                radio_layout.setVisibility(View.VISIBLE);
                look_detail_layout.setVisibility(View.VISIBLE);
                see_little_question_layout.setVisibility(View.VISIBLE);
                choose_work_score_lay.setVisibility(View.GONE);
                publish_work_score_layout.setVisibility(View.VISIBLE);
                refuseNum_layout.setVisibility(View.VISIBLE);
                choose_work_seework.setChecked(false);
                choose_work_seework_lay.setVisibility(View.GONE);
                see_little_question_text.setText(R.string.see_little_question_after_eff);
                mode_tips_text.setText(getResources().getString(R.string.lianxi_mode_tips));
                publish_work_mode_tetx.setText(getResources().getString(R.string.home_work_mode));
                work_eff_time_title.setText("截止时间");
                break;
            case RequestCode.TeaWorkContent.COMPUTER_ROOM_EXAM:
                isClassWork = false;
                //机房模式仅使用听说模考 在线作答 判断是否有其他题型
                int countListenSpeak = 0;
                int countOnlineAnswer = 0;
                for (int i = 0; i < publishWorkContentList.size(); i++) {
                    PublishWorkContent publishWorkContent = publishWorkContentList.get(i);
                    if (publishWorkContent.getModuleId() != ModuleInfo.ModuleInfoId.MODULE_ID_LISTEN_SPEAK_EXAM
                            && publishWorkContent.getModuleId() != ModuleInfo.ModuleInfoId.MODULE_ID_ONLINE_ANSWER) {
                        ToastUtil.INSTANCE.toastCenterError(R.string.choose_work_mode);
                        return;
                    } else if (publishWorkContent.getModuleId() == ModuleInfo.ModuleInfoId.MODULE_ID_LISTEN_SPEAK_EXAM) {
                        countListenSpeak++;
                    } else if (publishWorkContent.getModuleId() == ModuleInfo.ModuleInfoId.MODULE_ID_ONLINE_ANSWER) {
                        countOnlineAnswer++;
                    }
                }
                if (countListenSpeak > 0 && countOnlineAnswer > 0) {
                    ToastUtil.INSTANCE.toastCenterError(R.string.choose_work_mode_tips);
                    return;
                }
                if (publishWorkContentList.size() > 1) {
                    abMode = 1;
                }
                choose_work_condition_lay.setVisibility(View.VISIBLE);
                radio_layout.setVisibility(View.GONE);
                look_detail_layout.setVisibility(View.GONE);
                see_little_question_layout.setVisibility(View.VISIBLE);
                choose_work_score_lay.setVisibility(View.GONE);
                choose_work_seework.setChecked(false);
                choose_work_seework_lay.setVisibility(View.VISIBLE);
                choose_work_submit_lay.setVisibility(View.GONE);
                publish_work_score_layout.setVisibility(View.GONE);
                refuseNum_layout.setVisibility(View.GONE);
                see_little_question_check.setChecked(false);
                see_little_question_text.setText(R.string.can_not_see_answer);
                work_eff_time_title.setText("截止时间");
                showAnswerMode = 1;
                examMode = 1;
                submitMode = 1;
                //截止前可看
                seeWorkMode = 1;
                showScoreMode = 1;
                autoRefuseMode = 0;
                publish_work_mode_tetx.setText(getResources().getString(R.string.computer_room_exam));
                break;
            default:
                break;
        }
    }

    @Override
    protected void loadData() {
        BaseRequest.VerifyInfo verifyInfo = HttpRequest.getVerifyInfo();
        if (verifyInfo != null) {
            clientId = verifyInfo.getClientId();
            userId = EApplication.getInstance().getUserInfoBase().getUserId() + "";
            brandId = EApplication.BRAND_ID + "";
        }
        getMyOneSchool();
    }

    public void setPageCheckStatus() {
        Map<String, String> workNameMap = new LinkedHashMap<>();
        for (int i = 0; i < publishWorkContentList.size(); i++) {
            workNameMap.put(publishWorkContentList.get(i).getWorkName(), publishWorkContentList.get(i).getWorkName());
        }
        int index = 0;
        Set<String> names = workNameMap.keySet();
        for (String name : names) {
            if (index == 0) {
                workName = name;
            } else {
                workName = workName + "," + name;
            }
            index++;
        }
        publish_work_name.setText(workName);
        choose_work_methed1.setChecked(true);
        see_little_question_check.setChecked(true);
        choose_work_methed2.setChecked(false);
        choose_timing_publish.setChecked(false);
    }

    /**
     * 获取学校信息
     */
    public void getMyOneSchool() {
        SchoolDetail mySchool = EApplication.getInstance().getMySchool();
        if (null == mySchool) {
            return;
        }
        schoolId = mySchool.getSchoolId();
        schoolName = mySchool.getSchoolName();
        areaCode = mySchool.getAreaCode();
        getMyClass(brandId, mySchool.getSchoolId(), userRole);
    }

    /**
     * 获取班级列表
     */
    public void getMyClass(String brandId, int schoolId, int userRole) {
        EpaperDao.getInstance().getMyClass(brandId, userRole, schoolId, new BaseCallBack() {
            @Override
            public void failed(int errorCode, Object data) {

                if (data instanceof TimeoutError) {
                    hideCricleProgress();
                    ToastUtil.INSTANCE.toastCenterError(getString(R.string.load_faild_check_net_work));
                } else {
                    hideCricleProgress();
                    ToastUtil.INSTANCE.toastCenterError(getString(R.string.load_faild_try_later));
                }
            }

            @Override
            public void failed(Object data) {
                hideCricleProgress();
                if (!NetworkUtils.isOnline()) {
                    ToastUtil.INSTANCE.toastCenterError(getString(R.string.load_faild_check_net_work));
                    return;
                }
            }

            @Override
            public void success(Object data) {
                hideCricleProgress();
                if (null != data && data instanceof ArrayList) {
                    teacherClassBeans = (List<TeacherClassBean>) data;
                    teacherClassBeans.add(0, new TeacherClassBean(0, "全选", 0, "", 0, 0, 0, "", 0, ""));
                    publishWorkClassNameAdapter.setNewData(teacherClassBeans);
                }
            }
        });
    }

    /**
     * 设置接受作业的用户对象
     *
     * @param isChecked 是否选中
     */
    public void setUserList(Boolean isChecked, TeacherClassBean teacherClassBean) {
        if (isChecked) {
            //选中 将该班级对象设为全选
            if (pubLishClassBeanList.size() > 0) {
                if (teacherClassBean.getClassId() != 0) {
                    Boolean isReplace = false;
                    for (int i = 0; i < pubLishClassBeanList.size(); i++) {
                        if (pubLishClassBeanList.get(i).getClassId().equals(teacherClassBean.getClassId())) {
                            isReplace = true;
                            pubLishClassBeanList.remove(i);
                            getMembers(teacherClassBean.getClassId(), teacherClassBean.getStudentSize(), teacherClassBean.getClassName());
                            break;
                        }

                    }
                    //当前选择的发布作业对象不存在已有发布作业对象集合当中
                    if (!isReplace) {
                        getMembers(teacherClassBean.getClassId(), teacherClassBean.getStudentSize(), teacherClassBean.getClassName());
                    }
                }
            } else {
                if (teacherClassBean.getClassId() != 0) {
                    getMembers(teacherClassBean.getClassId(), teacherClassBean.getStudentSize(), teacherClassBean.getClassName());
                }
            }

            if (null != studentClassBeanList && studentClassBeanList.size() > 0) {
                for (int i = 0; i < studentClassBeanList.size(); i++) {
                    StudentClassBean studentClassBean = studentClassBeanList.get(i);
                    if (studentClassBean.getStudentClassList().get(0).getClassId() == teacherClassBean.getClassId()) {
                        for (int i1 = 0; i1 < studentClassBean.getStudentClassList().size(); i1++) {
                            studentClassBean.getStudentClassList().get(i1).setCheckCount(studentClassBean.getStudentClassList().get(i1).getList().size());
                            List<StudentBean> studentBeanList = studentClassBean.getStudentClassList().get(i1).getList();
                            for (int a = 0; a < studentBeanList.size(); a++) {
                                studentBeanList.get(a).setCheckStatus(1);
                            }
                        }
                    }
                }

            }

        } else {
            //未选中 取消该班级对象
            if (pubLishClassBeanList.size() > 0) {
                for (int i = 0; i < pubLishClassBeanList.size(); i++) {
                    if (pubLishClassBeanList.get(i).getClassId().equals(String.valueOf(teacherClassBean.getClassId()))) {
                        pubLishClassBeanList.remove(i);
                        break;
                    }

                }
                if (null != studentClassBeanList && studentClassBeanList.size() > 0) {
                    for (int i = 0; i < studentClassBeanList.size(); i++) {
                        StudentClassBean studentClassBean = studentClassBeanList.get(i);
                        if (studentClassBean.getStudentClassList().get(0).getClassId() == teacherClassBean.getClassId()) {
                            for (int i1 = 0; i1 < studentClassBean.getStudentClassList().size(); i1++) {
                                studentClassBean.getStudentClassList().get(i1).setCheckCount(0);
                                List<StudentBean> studentBeanList = studentClassBean.getStudentClassList().get(i1).getList();
                                for (int a = 0; a < studentBeanList.size(); a++) {
                                    studentBeanList.get(a).setCheckStatus(2);
                                }
                            }
                        }
                    }

                }
            }

        }
    }

    /**
     * 获取班级成员
     *
     * @param classId
     */
    public void getMembers(final int classId, final int stuCount, final String className) {
        PubLishClassBean pubLishClassBean = new PubLishClassBean();
        List<PublishStudentBean> publishStudentBeanList = new ArrayList<>();
        pubLishClassBean.setUserList(publishStudentBeanList);
        pubLishClassBean.setStudentCount(stuCount);
        pubLishClassBean.setSelectStudentLength(stuCount);
        pubLishClassBean.setClassName(className);
        pubLishClassBean.setClassId(classId + "");
        pubLishClassBean.setPublishType(0);
        pubLishClassBeanList.add(pubLishClassBean);

    }


    /**
     * 设置截止时间
     */
    public void setEffDate(Date date) {
        //Date today = new Date();
        //默认为当天的23：59
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            Date d = calendar.getTime();
            completeDate = d.getTime() / 1000;
            //设置text
            SimpleDateFormat format = new SimpleDateFormat("MM月dd日 HH:mm");
            Date effDate = new Date(completeDate * 1000);
            String effText = format.format(effDate);
            work_eff_time_text.setText(effText);
        } catch (Exception e) {

        }

    }

    /**
     * 设置时间选择器
     */
    private void setTime() {
        //显示时间
        Calendar selectedDate = Calendar.getInstance();
        //获取当前年月日时分
        Integer year = selectedDate.get(Calendar.YEAR);
        Integer month = selectedDate.get(Calendar.MONTH);
        Integer day = selectedDate.get(selectedDate.DAY_OF_MONTH);
        Integer minute = selectedDate.get(selectedDate.MINUTE);

        //控制开始时间
        final Calendar startDate = Calendar.getInstance();
        //控制结束时间
        Calendar endDate = Calendar.getInstance();
        //定时布置 默认晚当前时间一小时
        if (2 == timingOrEff) {
            //设置定时发布时间
            hour = selectedDate.get(selectedDate.HOUR_OF_DAY) + 1;
            startDate.set(year, month, day, hour, minute);
            selectedDate.set(year, month, day, hour, minute);
            endDate.set((year + 5), 11, 28, 23, 59);
        } else {
            //设置截止时间
            hour = 23;
            startDate.set(year, month, day, hour, 59);
            selectedDate.set(year, month, (day + default_eff_day), hour, 59);
            endDate.set((year + 5), 11, 28, 23, 59);
        }

        if (pvTime == null) {
            pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelect(Date date, View v) {//选中事件回调
                    // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null
                    if (1 == timingOrEff) {
                        //截止时间
                        if (date.getTime() < System.currentTimeMillis()) {
                            ToastUtil.INSTANCE.toastCenterError(R.string.eff_time_tips);
                            return;
                        }
                        work_eff_time_text.setText(getTimes(date));
                        completeDate = date.getTime() / 1000;
                    } else {
                        //定时布置时间
                        if (date.getTime() < System.currentTimeMillis()) {
                            ToastUtil.INSTANCE.toastCenterError(R.string.timing_tips);
                            return;
                        }
                        timing_publish_text.setText(getTimes(date));
                        sendDate = date.getTime() / 1000;
                        completeDate = date.getTime() / 1000 + default_eff_day * 24 * 60 * 60;
                        Date nowaDays = new Date((completeDate * 1000));
                        setEffDate(nowaDays);
                    }

                }
            }).setType(new boolean[]{true, true, true, true, true, false})  //年月日时分秒 的显示与否，不设置则默认全部显示
                    .setLabel("年", "月", "日", "时", "分", "秒")
                    .isCenterLabel(true)
                    //.setDividerColor(R.color.favorites_title)
                    .setContentTextSize(16)//字号
                    .setDate(selectedDate)
                    .setRangDate(startDate, endDate)
                    .setDecorView(null)
                    .build();
        } else {
            pvTime.setDate(selectedDate);
        }
    }

    /**
     * 日期格式化
     *
     * @param date
     * @return
     */
    private String getTimes(Date date) {//年月日时分格式
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日 HH:mm");
        return format.format(date);
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.timing_publish_text) {
            timingOrEff = 2;
            setTime();
            if (null != pvTime) {
                pvTime.show();
            }
        } else if (id == R.id.work_eff_time_text) {
            timingOrEff = 1;
            setTime();
            if (null != pvTime) {
                pvTime.show();
            }
        } else if (id == R.id.publish_work_mode_lay) {
            bottomMenuDialog.show();
        } else if (id == R.id.publish_work_auto_refuse_num) {
            refusebottomMenuDialog.show();
        } else if (id == R.id.publish_work_lay) {
            //避免重复点击
            if (ClickUtil.isFastDoubleClick()) {
                return;
            }
            if (0 == completeDate) {
                Date nowaDays = new Date();
                setEffDate(nowaDays);
            }
            if (isChickTime == 1) {
                if (sendDate > completeDate) {
                    ToastUtil.INSTANCE.toastCenterError(R.string.eff_timing_tips);
                    return;
                }
            }
            if (null != publish_work_name.getText().toString() && !"".equals(publish_work_name.getText().toString())) {
                workName = publish_work_name.getText().toString();
            } else {
                ToastUtil.INSTANCE.toastCenterError(R.string.set_work_name);
                return;
            }
            if (null == pubLishClassBeanList || pubLishClassBeanList.size() == 0) {
                ToastUtil.INSTANCE.toastCenterError(R.string.choose_work_object);
                return;
            }
            for (int i = 0; i < publishWorkContentList.size(); i++) {
                publishWorkContentList.get(i).setWorkType(examMode);
                //考试模式跟读次数只能设为1次
                if (examMode == 2) {
                    PublishWorkContent publishWorkContent = publishWorkContentList.get(i);
                    String requirementContent = publishWorkContent.getRequirementContent();
                    if (!TextUtils.isEmpty(requirementContent)) {
                        JSONObject parse = JSONObject.parseObject(requirementContent);
                        parse.put("readtimes", 1);
                        publishWorkContent.setRequirementContent(parse.toString());
                    }
                }
                //课堂讲练只支持跟读，在线作答、听说模考
                if (isClassWork && publishWorkContentList.get(i).getModuleId() != ModuleInfo.ModuleInfoId.MODULE_ID_REPEAT && publishWorkContentList.get(i).getModuleId() != ModuleInfo.ModuleInfoId.MODULE_ID_LISTEN_SPEAK_EXAM
                        && publishWorkContentList.get(i).getModuleId() != ModuleInfo.ModuleInfoId.MODULE_ID_ONLINE_ANSWER && publishWorkContentList.get(i).getModuleId() != ModuleInfo.ModuleInfoId.MODULE_ID_SENTENCE_REPEAT) {
                    ToastUtil.INSTANCE.toastCenterError(R.string.publish_class_work_tip);
                    //hideCricleProgress();
                    return;
                }
                if (workType == -999) {
                    if (publishWorkContentList.get(i).getModuleId() != ModuleInfo.ModuleInfoId.MODULE_ID_DUBBING) {
                        FavoritesBook favoritesBook = publishWorkContentList.get(i).getFavoritesBook();
                        workType = favoritesBook.getServiceId();
                        bookName = favoritesBook.getProductName();
                        subjectId = favoritesBook.getSubject();
                    }
                }

            }
            showCricleProgress();
            publishWork(Integer.parseInt(brandId), showAnswerMode, publishUserName, workName, workType, examMode, seeWorkMode, submitMode, showScoreMode, abMode, autoRefuseMode,
                    autoRefusePercent, autoRefuseNum, completeDate, sendDate, workMessageType, subjectId, Integer.valueOf(clientId), bookName, publishType, schoolId, schoolName, areaCode, pubLishClassBeanList, publishWorkContentList);

        } else if (id == R.id.upe_work_name_img) {
            publish_work_name.setText("");
        }
    }


    /**
     * 发布作业
     */
    private void publishWork(int brandId, int showAnswerMode, String publishUserName, final String workName, int workType, int examMode, int seeWorkMode,
                             int submitMode, int showScoreMode, int abMode, int autoRefuseMode, int autoRefusePercent, int autoRefuseNum, final long completeDate,
                             final long sendDate, int workMessageType, int subjectId, int sourceType, String bookName, int publishType, int schoolId, String schoolName, String areaCode,
                             final List<PubLishClassBean> classList, final List<PublishWorkContent> contents) {


        EpaperDao.getInstance().publishWork(brandId, showAnswerMode, publishUserName, workName, workType, examMode, seeWorkMode, submitMode, showScoreMode
                , abMode, autoRefuseMode, autoRefusePercent, autoRefuseNum, completeDate, sendDate, workMessageType, subjectId, sourceType, bookName, publishType, schoolId, schoolName, areaCode,
                classList, contents, isClassWork, new BaseExtCallBack(this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {
                    @Override
                    public void success(final Object data) {
                        //发布作业成功 清除保存本地发布作业内容
                        SerializableManager.getInstance().serialize(SerializableManager.SerializeKey.SHARE_KEY_CHOOSE_WORK_CONTENT, (Serializable) null);
                        SerializableManager.getInstance().serialize(SerializableManager.SerializeKey.SHARE_KEY_CHOOSE_WORK_MOUDEL, (Serializable) null);
                        PublishWorkResult mPublishWorkResult = (PublishWorkResult) data;
                        EpaperJumpManager.jumpToWorkSuccessActivity(TeaPublishWorkActivity.this, tag, JUMP_SORECE_TYPE_ME_SETTING, workName, sendDate, completeDate, classList, contents, mPublishWorkResult.getTermId());
                    }

                    @Override
                    public void failed(int errorCode, Object data) {
                        hideCricleProgress();
                        super.failed(errorCode, data);
                        failed(data);
                    }

                    @Override
                    public void failed(Object data) {
                        hideCricleProgress();
                        if (data instanceof TimeoutError) {
                            ToastUtil.INSTANCE.toastCenterError(R.string.time_out_try_late);
                        }
                    }
                });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (RESULT_OK == resultCode) {
            if (JUMP_TO_CHOOSE_GROUP == requestCode) {
                if (null != data) {
                    Boolean isReplace = false;
                    int count = 0;
                    studentBeanLists = (List<StudentGroupBean>) data.getSerializableExtra(IntentFlag.INTENT_FLAG_STUDENT_LIST);
                    if (null != studentClassBeanList && null != studentBeanLists && studentBeanLists.size() > 0) {
                        for (int i = 0; i < studentClassBeanList.size(); i++) {
                            List<StudentGroupBean> studentClassList = studentClassBeanList.get(i).getStudentClassList();
                            if (studentClassList.get(0).getClassId() == studentBeanLists.get(0).getClassId()) {
                                count++;
                                studentClassBeanList.remove(i);
                                StudentClassBean studentClassBean = new StudentClassBean();
                                studentClassBean.setStudentClassList(studentBeanLists);
                                studentClassBeanList.add(studentClassBean);
                                break;
                            }
                        }
                        if (count == 0) {
                            StudentClassBean studentClassBean = new StudentClassBean();
                            studentClassBean.setStudentClassList(studentBeanLists);
                            studentClassBeanList.add(studentClassBean);
                        }

                    }


                    PubLishClassBean pubLishClassBean = (PubLishClassBean) data.getSerializableExtra(IntentFlag.INTENT_FLAG_CHOOES_STU);
                    if (pubLishClassBean.getSelectStudentLength() != 0) {
                        for (int i = 0; i < teacherClassBeans.size(); i++) {
                            if (String.valueOf(teacherClassBeans.get(i).getClassId()).equals(pubLishClassBean.getClassId())) {
                                if (pubLishClassBean.getSelectStudentLength() == pubLishClassBean.getStudentCount()) {
                                    teacherClassBeans.get(i).setChecked(1);
                                    List<PublishStudentBean> userList = new ArrayList<>();
                                    pubLishClassBean.setUserList(userList);
                                    teacherClassBeans.get(i).setCheckCount(pubLishClassBean.getSelectStudentLength());
                                    break;
                                } else if (0 == pubLishClassBean.getSelectStudentLength()) {
                                    teacherClassBeans.get(i).setChecked(2);
                                    teacherClassBeans.get(i).setCheckCount(pubLishClassBean.getSelectStudentLength());
                                    break;
                                } else {
                                    teacherClassBeans.get(i).setChecked(3);
                                    teacherClassBeans.get(i).setCheckCount(pubLishClassBean.getSelectStudentLength());
                                    break;
                                }
                            }
                        }
                        publishWorkClassNameAdapter.notifyDataSetChanged();

                        if (pubLishClassBeanList.size() > 0) {
                            for (int i = 0; i < pubLishClassBeanList.size(); i++) {
                                if (pubLishClassBeanList.get(i).getClassId().equals(pubLishClassBean.getClassId())) {
                                    isReplace = true;
                                    pubLishClassBeanList.remove(i);
                                    pubLishClassBeanList.add(pubLishClassBean);
                                    break;
                                }

                            }
                            //当前选择的发布作业对象不存在已有发布作业对象集合当中
                            if (!isReplace) {
                                pubLishClassBeanList.add(pubLishClassBean);
                            }
                        } else {
                            pubLishClassBeanList.add(pubLishClassBean);
                        }

                    } else {
                        for (int i = 0; i < teacherClassBeans.size(); i++) {
                            if (String.valueOf(teacherClassBeans.get(i).getClassId()).equals(pubLishClassBean.getClassId())) {
                                teacherClassBeans.get(i).setChecked(2);
                                teacherClassBeans.get(i).setCheckCount(pubLishClassBean.getSelectStudentLength());
                                break;
                            }
                        }
                        publishWorkClassNameAdapter.notifyDataSetChanged();
                        if (pubLishClassBeanList.size() > 0) {
                            for (int i = 0; i < pubLishClassBeanList.size(); i++) {
                                if (pubLishClassBeanList.get(i).getClassId().equals(pubLishClassBean.getClassId())) {
                                    pubLishClassBeanList.remove(i);
                                }
                            }
                        }
                    }
                }
            }
        }

    }

}
